import "../static/libs/flexible"
import "../scss/common.scss"
import "../scss/test.scss"
import http from "../utils/http"

//首页传的类型id
const typeId = location.search.substr(1)

function main() {

    renderTesting() //渲染正在进行的考试
    renderTested() //渲染已完成的考试
    jump()
}
main()

//渲染正在进行的考试
async function renderTesting() {
    const { data } = await http({ url: "/tests/NotFinish", method: "POST" })
    const template = data.map(item => {
        return `
        <div class="item">
            <div class="pic">
                <img src="${item.pointId.img}" alt="">
            </div>
            <div class="info">
                <p class="p1">${item.pointId.knowledge}测试题<span>可参加</span></p>
                <p class="p2">2022-03-31 09:48~2022-04-03 09:48</p>
                <p class="p3"><i class="fa fa-clock-o"></i> 限时60分钟</p>
            </div>
            <button testId="${item._id}" class="btn" type="button">进入</button>
        </div>
        `
    })
    $(".container .box").html(template.join(""))
    toTest() //给正在考试的进入按钮绑定事件
}

//渲染已完成的考试
async function renderTested() {
    const { data } = await http({ url: "/testeds/findByStuId", method: "POST" })
    console.log(data);
    const template = data.map(item => {
        return `
        <div class="item">
            <div class="pic">
                <img src="${item.testId.pointId.img}" alt="">
            </div>
            <div class="info">
                <p class="p1">${item.testId.pointId.knowledge}<span>已完成</span></p>
                <p class="p2">2022-03-31 09:48~2022-04-03 09:48</p>
                <p class="p3"><i class="fa fa-clock-o"></i> 限时60分钟</p>
            </div>
            <button testedId="${item._id}" class="btn" type="button">查看</button>
        </div>
        `
    })
    $(".container .box2").html(template.join(""))
    //点击已完成考试的查看按钮进入解析页面
    toTestedAnalysis()
}

//给正在考试的进入按钮绑定事件进入考试页面
function toTest() {
    $(".container .testing .btn").on("click", function () {
        const testId = $(this).attr("testId")
        window.location.assign("startTest.html?" + typeId + "-" + testId)
    })
}

//点击已完成考试的查看按钮进入解析页面
function toTestedAnalysis() {
    $(".finish .item .btn").on("click", function () {
        const testedId = $(this).attr("testedId")
        window.location.assign("analysis.html?" + testedId)
    })
}

function jump() {
    $("#nav").on("click", function () {
        if ($(".header #nav .wrapper").css("display") == "none") {
            $(".header #nav .wrapper").css("display", "block")
            $(".header #nav .wrapper .menu li").on("click", function () {
                const text = $(this).text()
                console.log(text);
                if (text == "首页") {
                    window.location.assign("index.html")
                } else if (text == "错题本") {
                    window.location.assign("errors.html")
                } else if (text == "收藏夹") {
                    window.location.assign("collection.html")
                } else if (text == "登录") {
                    window.location.assign("login.html")
                } else if (text == "个人中心") {
                    window.location.assign("personalCenter.html")
                }
            })
        } else {
            $(".header #nav .wrapper").css("display", "none")
        }
    })
}